/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strconv"

	"github.com/spf13/cobra"
)

var node_enableCmd = &cobra.Command{
	Use: "enable",
	Run: func(cmd *cobra.Command, args []string) {
		client := http.Client{}
		req, err := http.NewRequest("GET", cmd_url_prefix+"/api/node/enable", nil)
		if err != nil {
			fmt.Printf("创建请求失败: %v\n", err)
			return
		}

		q := req.URL.Query()
		node, _ := cmd.Flags().GetString("node")
		enable, _ := cmd.Flags().GetBool("enable")
		q.Add("node", node)
		q.Add("enable", strconv.FormatBool(enable))
		req.URL.RawQuery = q.Encode()

		resp, err := client.Do(req)
		if err != nil {
			fmt.Printf("请求失败: %v\n", err)
			return
		}
		defer resp.Body.Close()
		buf, _ := ioutil.ReadAll(resp.Body)
		fmt.Print(string(buf))
	},
}

// serviceCmd represents the service command
var nodeCmd = &cobra.Command{
	Use: "node",
	Run: func(cmd *cobra.Command, args []string) {
		cmd.Help()
	},
}

func init() {
	rootCmd.AddCommand(nodeCmd)

	nodeCmd.AddCommand(node_enableCmd)
	node_enableCmd.Flags().String("node", "", "node name")
	node_enableCmd.Flags().Bool("enable", true, "true/false")

	node_enableCmd.MarkFlagRequired("enable")
}
